#include "AppSence.h"
#include "AppLayer.h"
#include "AppDefine.h"


CCMenuItem *theSaveItem,*theDelItem,*theAddItem,*theUpdateItem;

AppSence::AppSence(bool bPortrait)
:m_bPortrait(bPortrait)
{
	if (m_bPortrait)
	{
		CCDirector::sharedDirector()->setDeviceOrientation(CCDeviceOrientationLandscapeRight);
	}
    
	CCScene::init();
    
	CCSize s = CCDirector::sharedDirector()->getWinSize();
	win_width = s.width;
	win_height = s.height;
	scale = win_width/TOT_COL_NUM/cell_width;
	
	//set the background image
	CCRect r(0, 0, win_width, win_height);
	CCSprite* sp = CCSprite::spriteWithFile(bgfile, r);
	ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT,GL_REPEAT};
	sp->getTexture()->setTexParameters(&tp);
	sp->setPosition(ccp(win_width/2, win_height/2));
	this->addChild(sp);
    
}

void AppSence::onEnter()
{
	CCScene::onEnter();
	
	/*
	CCSprite *botTop=CCSprite::spriteWithFile(barfile); 
	botTop->setScale(scale);
	botTop->setPosition(ccp(win_width/2, menu_height*scale/2) );
	this->addChild(botTop, 0);
	
    
	CCSize s = CCDirector::sharedDirector()->getWinSize();
	CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWithFile("z.png");  
	this->addChild(spriteBatch);  
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("z.plist"); 
	
	CCSprite* newNormalSprite = CCSprite::spriteWithSpriteFrameName("_add.png");
	CCSprite* newSelectedSprite = CCSprite::spriteWithSpriteFrameName("_add.png");
	CCSprite* newDisabledSprite = CCSprite::spriteWithSpriteFrameName("_add.png");
	CCMenuItemSprite* newItem = CCMenuItemSprite::itemFromNormalSprite(newNormalSprite, newSelectedSprite, newDisabledSprite, this, menu_selector(AppSence::menuAddCallback) );
	newItem->setScale(scale);
	theAddItem = newItem;
    
	CCSprite* saveNormalSprite = CCSprite::spriteWithSpriteFrameName("_save.png");
	CCSprite* saveSelectedSprite = CCSprite::spriteWithSpriteFrameName("_save.png");
	CCSprite* saveDisabledSprite = CCSprite::spriteWithSpriteFrameName("_save.png");
	CCMenuItemSprite* saveItem = CCMenuItemSprite::itemFromNormalSprite(saveNormalSprite, saveSelectedSprite, saveDisabledSprite, this, menu_selector(AppSence::menuSaveCallback) );
	theSaveItem=saveItem;
	saveItem->retain();
	theSaveItem->setIsEnabled(false);
	saveItem->setScale(scale);
    
	CCSprite* delNormalSprite = CCSprite::spriteWithSpriteFrameName("_del.png");
	CCSprite* delSelectedSprite = CCSprite::spriteWithSpriteFrameName("_del.png");
	CCSprite* delDisabledSprite = CCSprite::spriteWithSpriteFrameName("_del.png");
	CCMenuItemSprite* delItem = CCMenuItemSprite::itemFromNormalSprite(delNormalSprite, delSelectedSprite, delDisabledSprite, this, menu_selector(AppSence::menuClearCallback) );
	delItem->setScale(scale);
	theDelItem = delItem;
    
	CCSprite* infoNormalSprite = CCSprite::spriteWithSpriteFrameName("_info.png");
	CCSprite* infoSelectedSprite = CCSprite::spriteWithSpriteFrameName("_info.png");
	CCSprite* infoDisabledSprite = CCSprite::spriteWithSpriteFrameName("_info.png");
	CCMenuItemSprite* infoItem = CCMenuItemSprite::itemFromNormalSprite(infoNormalSprite, infoSelectedSprite, infoDisabledSprite, this, menu_selector(AppSence::menuSynCloudCallback) );
	infoItem->setScale(scale);
    
    
	bottomMenu =CCMenu::menuWithItems(newItem,saveItem,delItem,infoItem,NULL);
	bottomMenu->alignItemsHorizontallyWithPadding(10);
	bottomMenu->setPosition( ccp(s.width/2,menu_height*scale/2) );
    addChild(bottomMenu, 1);
    */
}

void AppSence::menuCloseCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();
}


void AppSence::menuAddCallback(CCObject* pSender)
{
	//CCScene* pScene = new AppSence();
	
	//CCMenuItemSprite* saveItem = (CCMenuItemSprite*)(bottomMenu->getChildByTag(1001));//->setIsEnabled(false);
	theSaveItem->setIsEnabled(!theSaveItem->getIsEnabled());
	theDelItem->setIsEnabled(!theDelItem->getIsEnabled());
	//theUpdateItem->setIsEnabled(!theUpdateItem->getIsEnabled());
	theAddItem->setIsEnabled(!theAddItem->getIsEnabled());
	//this->removeChildByTag(0,true);
    

}

void AppSence::menuListCallback(CCObject* pSender)
{
	CCScene* pScene = new AppSence();
    AppLayer* pLayer = new AppLayer();
	pScene->addChild( pLayer, 0 );
	pLayer->sence = pScene;
    
	//CCDirector::sharedDirector()->replaceScene( CCTransitionFlipX::transitionWithDuration(2, pScene) );
	CCDirector::sharedDirector()->replaceScene(pScene );
    pScene->release();
    
    pLayer->release();
}

void AppSence::menuSynCloudCallback(CCObject* pSender)
{
	
    
}


void AppSence::menuClearCallback(CCObject* pSender)
{
	 
    
}

void AppSence::menuUpdateCallback(CCObject* pSender)
{
 
    
}

void AppSence::menuSaveCallback(CCObject* pSender)
{
	 
}

void AppSence::menuDelCallback(CCObject* pSender)
{

    
}